package com.example.memolaga.accounts;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.memolaga.Constants;
import com.example.memolaga.Functions;
import com.example.memolaga.R;
import com.example.memolaga.password.FunctionsPass;

public class AccEdit extends Activity {

    private String sKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getIntent().getExtras();
        String accName = "";
        setContentView(R.layout.acc_edit);
        if (bundle!=null)
        {
            this.sKey = bundle.getString(Constants.getKeyString());
            accName = bundle.getString(Constants.getAccountName());
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
            {
                ((ImageView) findViewById(R.id.imgAccUserEdit)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editAccUsernameEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                ((ImageView) findViewById(R.id.imgAccPassEdit)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editAccPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
            }
        }
        else
            Functions.showLongToast(getApplicationContext(), getString(R.string.str_error_loading_inf));
        if (!accName.equals(""))
        {
            AccountsMaster acc = AccountsManager.getInstance().getAccount(accName);
            ((EditText) findViewById(R.id.editAccServerEdit)).setText(acc.getServer());
            ((EditText) findViewById(R.id.editAccPathEdit)).setText(acc.getPath());
            ((EditText) findViewById(R.id.editAccUsernameEdit)).setText(FunctionsPass.decryptString(sKey, acc.getUsername()));
            ((EditText) findViewById(R.id.editAccPassEdit)).setText(FunctionsPass.decryptString(sKey,acc.getPassword()));
            ((EditText) findViewById(R.id.editAccProviderEdit)).setText(acc.getProvider());
            ((EditText) findViewById(R.id.editAccProvWebEdit)).setText(acc.getProviderWebSite());
            ((EditText) findViewById(R.id.editAccProvPhoneEdit)).setText(acc.getProviderPhone());
            ((EditText) findViewById(R.id.editAccNoteEdit)).setText(acc.getNote());
            ((Button) findViewById(R.id.btnAccSaveEdit)).setOnClickListener(btnSaveAccEdit);
        }
        ((ImageView) findViewById(R.id.imgAccUserEdit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((EditText) findViewById(R.id.editAccUsernameEdit)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                {
                    ((ImageView) findViewById(R.id.imgAccUserEdit)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                    ((EditText) findViewById(R.id.editAccUsernameEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                else
                {
                    ((ImageView) findViewById(R.id.imgAccUserEdit)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                    ((EditText) findViewById(R.id.editAccUsernameEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });
        ((ImageView) findViewById(R.id.imgAccPassEdit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((EditText) findViewById(R.id.imgAccPassEdit)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                {
                    ((ImageView) findViewById(R.id.imgAccPassEdit)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                    ((EditText) findViewById(R.id.editAccPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                else
                {
                    ((ImageView) findViewById(R.id.imgAccPassEdit)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                    ((EditText) findViewById(R.id.editAccPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                }
            }
        });
    }

    private View.OnClickListener btnSaveAccEdit = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = ((TextView) findViewById(R.id.txtAccNameEdit)).getText().toString();
            String server = ((EditText) findViewById(R.id.editAccServerEdit)).getText().toString();
            String path = ((EditText) findViewById(R.id.editAccPathEdit)).getText().toString();
            String userName = ((EditText) findViewById(R.id.editAccUsernameEdit)).getText().toString();
            String pass = ((EditText) findViewById(R.id.editAccPassEdit)).getText().toString();
            String provider = ((EditText) findViewById(R.id.editAccProviderEdit)).getText().toString();
            String provWebSite = ((EditText) findViewById(R.id.editAccProvWebEdit)).getText().toString();
            String provPhone = ((EditText) findViewById(R.id.editAccProvPhoneEdit)).getText().toString();
            String note = ((EditText) findViewById(R.id.editAccNoteEdit)).getText().toString();
            AccountsManager.getInstance().accMod(name, server, path, userName, pass, provider, provWebSite, provPhone, note, sKey);
            Functions.showLongToast(getBaseContext(), getString(R.string.strOKCreateFile));
            setResult(Constants.getResultEdit());
            finish();
        }
    };

/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.acc_edit, menu);
        return true;
    }
    */
}
